package yl.hs.bmipfull.controllers.salemanage;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.salemanage.CustomerInfoEntity;
import yl.hs.bmipfull.service.salemanage.ICustomerInfoService;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.List;

/**
 * 客户信息管理的控制层
 *
 * @author 谢景广
 * @date 2021-05-18
 */
@Api(value = "客户信息管理Web接口", tags = "销售管理——客户信息管理")
@RequestMapping(path = "/api/customers/info")
@RestController
public class CustomerController extends ControllerBase {

    @Autowired
    ICustomerInfoService customerSvc;
    @Autowired
    ISearchViewService svSVC;

    @ApiOperation(value = "获取客户信息", notes = "根据搜索条件获取客户信息")
    @RequestMapping(value = {"state/{state}",""}, method = RequestMethod.GET)
    public WebResult getList(@ApiParam("状态") @PathVariable(required = false,name = "state") Short state) {
        return success("ok", customerSvc.getList(state));
    }

    @ApiOperation(value = "验证客户信息", notes = "验证客户信息是否可以保存")
    @RequestMapping(value = "valida", method = RequestMethod.GET)
    public WebResult customerValida(@ApiParam("客户名称") @RequestParam String customerName,@ApiParam("编号") @RequestParam String sn) {
        boolean result=false;
        if(sn==null || sn.equals("null") || sn==""){
            result=customerSvc.customerValida(customerName,null);
        }else{
            result=customerSvc.customerValida(customerName,sn );
        }
        return success("ok", result);
    }

    @ApiOperation(value = "根据查询条件获取客户的分页信息", notes = "获取客户的分页信息")
    @RequestMapping(path = "{rowNum}/{page}", method = RequestMethod.POST)
    @ResponseBody
    public WebResult query(@PathVariable int rowNum, @PathVariable int page, @RequestBody PageQuery query) {

        try {
            PageResult<CustomerInfoEntity> result = customerSvc.queryPage(query, rowNum, page);
            svSVC.updateUserMenuView(query);
            return success("ok", result);
        }
        catch (Exception ex){
            return error(ex);
        }
    }



    @ApiOperation(value = "保存新建或者编辑的客户信息", notes = "保存客户信息")
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public WebResult save(@RequestBody SubmitModel form) {

        try {
            Integer result = customerSvc.saveCustomer(form);
            if (result > 0) {
                return success("客户信息已保存",form.getSn());
            } else {
                return fail("客户信息保存失败");
            }
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "根据客户编号获取客户信息", notes = "获取客户信息")
    @RequestMapping(value = "{sn}", method = RequestMethod.GET)
    @ResponseBody
    public WebResult find(@PathVariable String sn) {
        try {
            CustomerInfoEntity customer = customerSvc.findBySN(sn);
            if (customer != null) {
                return success("ok", customer);
            }
            return notfound(String.format("没有找到编号%s的客户信息", sn));
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "根据客户编号删除客户信息", notes = "删除客户信息")
    @RequestMapping(value = "{sn}", method = RequestMethod.DELETE)
    @ResponseBody
    public WebResult delete(@PathVariable String sn) {
        try {
            Integer result = customerSvc.deleteBySn(sn.split(","));
            if (result > 0) {
                return success("数据已删除");
            } else if (result == -2) {
                return fail("存在合同信息，不允许删除！");
            }
            return fail("没有删除任何数据");
        } catch (Exception ee) {
            return error(ee);
        }
    }


    @ApiOperation(value = "根据客户编号删除客户信息", notes = "删除客户信息")
    @RequestMapping(value = "/recovery", method = RequestMethod.DELETE)
    @ResponseBody
    public WebResult recovery(List<String> sn) {
        try {
            if (customerSvc.recovery(sn)) {
                return success("数据已恢复");
            } else {
                return fail("数据不存在");
            }
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "根据客户编号删除客户信息", notes = "删除客户信息")
    @RequestMapping(value = "/changeStateBySn/{sn}/{state}", method = RequestMethod.GET)
    @ResponseBody
    public WebResult changeStateBySn(@PathVariable String sn,@PathVariable short state) {
        try {
            Integer result = customerSvc.changeStateBySn(sn.split(","), state);
            if (result > 0) {
                return success("数据已修改");
            } else if (result < -2) {
                return fail("数据修改失败！");
            }
            return fail("没有修改的任何数据");
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "转换客户所有权", notes = "转换客户所有权")
    @RequestMapping(value = "/transfer/{mainSN}/{userSN}", method = RequestMethod.GET)
    @ResponseBody
    public WebResult customerTransfer(@PathVariable String mainSN,@PathVariable String userSN) {
        try {
            Integer result = customerSvc.customerTransfer(mainSN, userSN);
            if (result > 0) {
                return success("权限已转移");
            } else if (result < -2) {
                return fail("数据修改失败！");
            }
            return fail("没有修改的任何数据");
        } catch (Exception ee) {
            return error(ee);
        }
    }


}
